<template>
  <div class="app-container">
    <div class="nav-search">
      <el-input v-model="searchParams.clinicName" placeholder="诊所名称"></el-input>
      <el-date-picker
        v-model="createDate"
        :clearable="false"
        type="date"
        value-format="yyyy-MM-dd"
        format="yyyy-MM-dd"
        placeholder="创建日期"
        @change="createDateChange(createDate)"
      >
      </el-date-picker>
      <span>
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
        <el-button class="filter-item" size="mini" type="warning" icon="el-icon-search" @click="toReset">重置</el-button>
        <el-button class="filter-item" size="mini" type="warning" icon="el-icon-search" @click="exportList">导出</el-button>
      </span>
      <div class="tableHandle">
        <el-button class="filter-item" size="mini" type="success" @click="myAddress">我的地址</el-button>
      </div>
    </div>
    <div class="tableList">
      <el-table
        ref="table"
        v-loading="loading"
        stripe
        :data="tableData"
        size="small"
        style="width: 100%;"
        @selection-change="selectionChangeHandler"
      >
        <el-table-column type="selection"></el-table-column>
        <el-table-column prop="clinicName" label="诊所名称"></el-table-column>
        <el-table-column prop="receiveType" label="收件类型">
          <template slot-scope="scope">
            <span>{{ dict.label.receive_type[scope.row.receiveType] }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="receiveContacts" label="收件人"></el-table-column>
        <el-table-column prop="receiveAddress" label="收件地址"></el-table-column>
        <el-table-column prop="receiveMobile" label="联系电话"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        background
        :page-size.sync="page.size"
        :total="total"
        :current-page.sync="page.page"
        style="margin-top: 8px; text-align: right;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChangeHandler($event)"
        @current-change="pageChangeHandler"
      ></el-pagination>
    </div>
    <!-- dialong弹窗 -->
    <div class="dialog">
      <el-dialog
        :visible.sync="dialogVisible"
        width="40%"
        append-to-body
        :modal="false"
      >
        <span slot="title" class="dialog-title">{{ dialogTitle }}</span>
        <div class="dialogBody">
          <el-form label-width="100px" size="mini">
            <div v-if="dialogTitle == '我的地址'">
              <el-form-item label="寄件人名称">
                <el-input v-model="formParams.receiver" placeholder="请输入"></el-input>
              </el-form-item>
              <el-form-item label="寄件地址">
                <el-input v-model="formParams.address" placeholder="请输入"></el-input>
              </el-form-item>
              <el-form-item label="联系电话">
                <el-input v-model="formParams.mobile" placeholder="请输入"></el-input>
              </el-form-item>
            </div>
            <div v-if="dialogTitle == '编辑类型'">
              <el-row style="margin-bottom: 10px;padding-left: 16px;">
                <el-col :span="8">
                  <el-radio v-model="editParams.receiveType" label="clinic">诊所</el-radio>
                </el-col>
                <el-col :span="8">
                  <el-radio v-model="editParams.receiveType" label="doctor">医生</el-radio>
                </el-col>
                <el-col :span="8">
                  <el-radio v-model="editParams.receiveType" label="agency">代理商</el-radio>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="6">
                  <el-form-item label-width="60px" label="省份:">
                    <p style="line-height: 0px;">{{ editParams.proCode }}</p>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label-width="60px" label="城市:">
                    <p style="line-height: 0px;">{{ editParams.cityCode }}</p>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label-width="60px" label="区域:">
                    <p style="line-height: 0px;">{{ editParams.areaCode }}</p>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label-width="60px" label="销售:">
                    <p style="line-height: 0px;">{{ editParams.salesId }}</p>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label-width="88px" label="诊所名称:">
                    <p style="line-height: 0px;">{{ editParams.clinicName }}</p>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label-width="88px" label="诊所编号:">
                    <p style="line-height: 0px;">{{ editParams.id }}</p>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label-width="88px" label="诊所地址:">
                    <p style="line-height: 0px;">{{ editParams.receiveAddress }}</p>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label-width="116px" label="矫治器收件人:">
                    <p style="line-height: 0px;">{{ editParams.receiveContacts }}</p>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label-width="88px" label="收件地址:">
                    <p style="line-height: 0px;">{{ editParams.receiveAddress }}</p>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label-width="88px" label="收件电话:">
                    <p style="line-height: 0px;">{{ editParams.receiveMobile }}</p>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>

          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogClose">取 消</el-button>
          <el-button type="primary" @click="dialogSave">保 存</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { logisticsQueryPage, logisticsDownload, lmAddressQueryPage, addLmAddress, updataClinic } from '@/api/logisticsManage';
import { queryClinic } from '@/api/aid';
import { mapGetters } from 'vuex';
import { parseTime } from '@/utils/index';

export default {
  components: {},
  dicts: [
    'receive_type'  // 收件类型
  ],
  data() {
    return {
      loading: false,
      dialogVisible: false,
      dialogTitle: '',
      searchParams: {
        clinicName: '', // 诊所名称
        startDate: '', // 创建开始
        endDate: '' // 创建结束
      },
      page: {
        page: 0,
        size: 10
      },
      tableData: [],
      total: 0,
      formParams: {
        receiver: '',  // 寄件人名称
        address: '',  // 地址
        mobile: ''  // 电话
      },
      editParams: {
        receiveType: '',  // 收件类型
        createUser: ""  // 创建人id
      },
      radio: 1,
      multipleSelection: [],
      createDate: "",  // 创建时间
      lmAddressList: []  // 罗慕地址数据
    };
  },
  computed: {
    ...mapGetters(['searchInfo', 'user'])
  },
  watch: {},
  mounted() {
    this.getLogisticsList({ pageIndex: 0, pageSize: 10 });
    this.getlmAddressQueryPage();
    console.log("收件类型----------",this.dict.receive_type);
  },
  methods: {
    parseTime,
    // 全选
    selectionChangeHandler(val) {
      this.multipleSelection = val;
    },
    // 获取列表
    getLogisticsList(params) {
      this.loading = true;
      logisticsQueryPage(params)
        .then(res => {
          this.tableData = res.content || [];
          this.total = res.totalElements || 0;
          this.loading = false;
        })
        .catch(er => {
          this.loading = false;
        });
    },
    // 获取罗慕地址数据
    getlmAddressQueryPage(){
      lmAddressQueryPage({}).then(res => {
        console.log("eeeeeeeeee-----",res);
        this.lmAddressList = res;
        this.formParams.receiver = this.lmAddressList[this.lmAddressList.length - 1].receiver;
        this.formParams.address = this.lmAddressList[this.lmAddressList.length - 1].address;
        this.formParams.mobile = this.lmAddressList[this.lmAddressList.length - 1].mobile;
      })
    },
    // 创建日期change
    createDateChange(date) {
      console.log("dddddddddddddddddd",date);
      this.searchParams.startDate = date;
      this.searchParams.endDate = date;
    },
    // 搜索
    toQuery() {
      const search = {
        pageIndex: this.page.page - 1,
        pageSize: this.page.size,
        ...this.searchParams
      };
      this.getLogisticsList(search);
    },
    // 重置
    toReset() {
      this.searchParams.clinicName = '';
      this.createDate = '';
      this.searchParams.startDate = '';
      this.searchParams.endDate = '';
      this.toQuery();
    },
    // 我的地址
    myAddress() {
      this.dialogVisible = true;
      this.dialogTitle = '我的地址';
    },
    // 编辑
    handleEdit(index, row) {
      this.dialogVisible = true;
      this.dialogTitle = '编辑类型';
      this.editParams = JSON.parse(JSON.stringify(row));
      console.log("编辑----------",row,this.editParams);
    },
    sizeChangeHandler() {
      this.toQuery();
    },
    // 页数变更
    pageChangeHandler(pageIndex) {
      this.toQuery();
    },
    // 导出
    exportList() {
      logisticsDownload(this.searchParams).then(res => {
        const url = window.URL.createObjectURL(res);
        var a = document.createElement('a');
        document.body.appendChild(a);
        a.href = url;
        a.download = '物流管理数据.xlsx';
        a.click();
        window.URL.revokeObjectURL(url);
      }).catch(er => {});
    },
    // 取消弹框
    dialogClose() {
      this.dialogVisible = false;
    },
    // 保存
    dialogSave() {
      if(this.dialogTitle == "我的地址"){
        addLmAddress(this.formParams).then(res => {
          console.log("我的地址------成功！！");
          this.getlmAddressQueryPage();
        })
      }else if(this.dialogTitle == "编辑类型"){
        this.editParams.createUser = this.user.id
        updataClinic(this.editParams).then(res => {
          console.log("编辑类型-------成功！！");
          this.getLogisticsList({ pageIndex: 0, pageSize: 10 });
        })
      }
      this.dialogVisible = false;
    }
  }
};
</script>
<style scoped lang='scss'>
.nav-search {
  padding: 10px 0;
  .el-input {
    width: 320px;
    margin-right: 10px;
  }
  .el-date-editor--date {
    width: 150px;
  }
}
.tableHandle {
  float: right;
  margin: 0;
  padding: 0;
  margin-top: 4px;
}
.searchForm {
  width: 49%;
  display: inline-block;
  margin: 5px 0px;
}
.dialog {
  .dialog-title {
    color: #fff;
  }
}
</style>
